package net.htjs.mobile.player;

import java.util.List;

import net.htjs.mobile.Chess;
import net.htjs.mobile.ChessBoard;
import net.htjs.mobile.interfaces.BasePlayerDao;

import android.util.Log;


public class BasePlayer implements BasePlayerDao{
	private List<Chess> myChess;		//我下的所有棋子
	private boolean myFlag;           //我棋子的代号 
	private int myChessFlag;
	private boolean mettle ;				//玩家性格
	public static final boolean activist = true;	//激进（优先进攻）
	public static final boolean conservatism = false;//保守（优先防守）
	

	public BasePlayer(boolean myFlag) {
		this.myFlag = myFlag;
		Log.i("构造函数", "base player");
	}
	
	public boolean getMyFlag() {
		return myFlag;
	}

	public void setMyFlag(boolean myFlag) {
		this.myFlag = myFlag;
	}

	public int getMyChessFlag() {
		return myChessFlag;
	}

	public void setMyChessFlag(int myChessFlag) {
		this.myChessFlag = myChessFlag;
	}

	@Override
	public boolean hasWin(Chess lastchess, boolean whoschess ,int[][] chessArray) {
		int count ;
		int playerFlag = whoschess?ChessBoard.BLACK:ChessBoard.WHITE;
		int x = lastchess.getX();
		int y = lastchess.getY();
		int n ;
		//横向
		count = 1;
		while ((x+count)<ChessBoard.maxX && (chessArray[x+count][y]==playerFlag || chessArray[x+count][y]==playerFlag+2)) {
			count++;
			if (count>=5) {
				return true;
			}
		}
		for (n=x-1;n >= 0 && (chessArray[n][y]==playerFlag || chessArray[n][y]==playerFlag+2);n--) {
			count++;
			if (count>=5) {
				return true;
			}
		}
		//纵向
		count =1;
		while ((y+count)<ChessBoard.maxY && (chessArray[x][y+count]==playerFlag||chessArray[x][y+count]==playerFlag+2)) {
			count++;
			if (count>=5) {
				return true;
			}
		}
		for (n=y-1;n >= 0 && (chessArray[x][n]==playerFlag || chessArray[x][n]==playerFlag+2);n--) {
			count++;
			if (count>=5) {
				return true;
			}
		}
		//正斜向 ‘/’
		count =1;
		while ((x+count)<ChessBoard.maxX && (y-count)>=0 && (chessArray[x+count][y-count]==playerFlag||chessArray[x+count][y-count]==playerFlag+2)) {
			count++;
			if (count>=5) {
				return true;
			}
			n++;
		}
		for (n=1;x-n >= 0 && (y+n) <ChessBoard.maxY && (chessArray[x-n][y+n]==playerFlag || chessArray[x-n][y+n]==playerFlag);n++) {
			count++;
			if (count>=5) {
				return true;
			}
		}
		//反斜向 ‘\’
		count =1;
		while ((x-count)>=0 && (y-count)>=0 && (chessArray[x-count][y-count]==playerFlag||chessArray[x-count][y-count]==playerFlag+2)) {
			count++;
			if (count>=5) {
				return true;
			}
			n++;
		}
		for (n=1;(y+n)<ChessBoard.maxY && (x+n)<ChessBoard.maxX && (chessArray[x+n][y+n]==playerFlag || chessArray[x+n][y+n]==playerFlag+2);n++) {
			count++;
			if (count>=5) {
				return true;
			}
		}
		return false;
	}
	@Override
	public Chess run() {
		// TODO Auto-generated method stub
		return null;
	}
  
   public boolean isMyChess(int x, int y, int playerFlag){
     if(ChessBoard.chessArray[x][y]==playerFlag || ChessBoard.chessArray[x][y]==(playerFlag + 2)){
       return true;
     }
     return false;
   }
   
   public boolean isMyChess(Chess chess){
	   return this.isMyChess(chess.getX(), chess.getY(), this.myChessFlag);
   }

	   public boolean isInRange(int x, int y){
     if(x>=BaseAI.CalculateRange.xstart && y>=BaseAI.CalculateRange.ystart && x<=BaseAI.CalculateRange.xstop && y<=BaseAI.CalculateRange.ystop){
       return true;
     }
     return false;
   }

}
